<template>
  <div>
    <!-- 新增弹框 -->
    <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="roleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="cancel">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { addRoleManagement, updateRole } from '@/api/setting'

export default {
  props: {
    dialogTableVisible: {
      type: Boolean,
      default: false
    },
    isEditor: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },

  watch: {
    isEditor: {
      handler(newVal) {
        if (newVal) {
          this.roleForm = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },

  methods: {
    cancel() {
      this.$emit('update:dialogTableVisible', false)
    },
    // 确定
    async hSubmit() {
      const res = await (this.isEditor ? updateRole(this.roleForm) : addRoleManagement(this.roleForm))
      if (res.code === 10000) {
        this.$emit('update:dialogTableVisible', false)
        this.$emit('success')
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
